package 数论.约数个数;

import java.util.ArrayList;
import java.util.Queue;
import java.util.Scanner;

public class Hankson的趣味题优化版 {
    static long ret = 0;
    static long a, b, c, d;
    static int cnt2 = 0, cnt3 = 0;
    static long[] cnt = new long[1601];
    static long[] primes = new long[1601];
    static boolean[] st = new boolean[100000];
    static ArrayList<Pair> arr = new ArrayList<>(2000);

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        //先初始化
        is_prime(10000);
        int t = sc.nextInt();
        while(t-- >= 0) {
            ret = 0;

            a = sc.nextLong();
            b = sc.nextLong();
            c = sc.nextLong();
            d = sc.nextLong();
            arr.clear();
            //枚举d的质数问题
            long x = d;
            for(int i = 1; primes[i] <= x / primes[i]; ++ i ) {
                int tt = 0;
                if(d % primes[i] == 0) {
                    while(d % primes[i] == 0) {
                        d /= primes[i];
                        tt++;
                    }
                    arr.add(new Pair(primes[i], tt));
                }
            }
            if(x > 1) {
                arr.add(new Pair(x, 1));
            }
            dfs(0, 1);
        }
        System.out.println(ret);

    }
    public static void dfs(int len, long sum) {//因为约数的个数是非常小的 因此可以去考虑枚举约数最多才1600个
        if(len == arr.size()) {
            if(check(sum))ret++;
            return;
        }
        for(int i = 0; i <= arr.get(len).second; ++ i ) {
            dfs(len + 1, sum * ksm(arr.get(len).first, i));
        }
    }
    public static long gcd(long n, long m) {
        return m != 0 ? gcd(m, n % m) : n;
    }
    public static long lcm(long n, long m) {
        return n * m / gcd(n, m);
    }
    public static boolean check(long sum) {
        return gcd(sum, a) == b && lcm(sum, c) == d;
    }
    public static long ksm(long a, long b) {
        long t = 1;
        while(b != 0) {
            if((b & 1) != 0)t = t * a;
            a = a * a;
            b >>= 1;
        }
        return t;
    }
    public static void is_prime(long n) {
        for(int i = 2; i <= n; ++ i ) {
            if(st[i] == false) {
                primes[++cnt2] = i;
            }
            for(int j = 1; primes[j] <= n / i; ++ j ) {
                st[(int)(i * primes[j])] = true;
                if(i % primes[j] == 0)break;
            }
        }
    }
}
class Pair {
    long first;
    long second;

    public Pair(long first, long second) {
        this.first = first;
        this.second = second;
    }
}
/*
1
41 1 96 288
 */
